<?php defined('SYSPATH') or die('No direct script access.');

/**
 * @file type.php
 * @brief 类别管理
 * @author Lionel Tsai
 * @version 1.0
 * @date 2012-02-17
 */
class Controller_Type extends Controller_Rest {
	public function before()
   	{
		parent::before();
	}
	/**
	 * @brief 获取类别，包括一个和多个类别
	 *
	 * @param $type_id
	 *
	 * @return $this 
	 */
	public function action_get($type_id = NULL) 
	{
		if($type_id === NULL) {
			$this->get_types();
		}
		else {
			$this->get_type($type_id);
		}
	}
	
	/**
	 * @brief 获取指定类别
	 *
	 * @param $type_id
	 *
	 * @return $this
	 */
	private function get_type($type_id) 
	{
		$type = ORM::factory('type', $type_id);
		if(!$type->type_id) {
			$this->response->body("type not found!")->status(206);
			return;
		}
		$ret_type = json_encode($type->as_array());
		$this->response->body($ret_type)->status(200);
	}
	
	/**
	 * @brief 获取指定行数 指定类型的所有类别
	 *
	 * @return	$this 
	 */
	private function get_types() 
	{
		/** 获取起止条数*/
		$start = filter_input(INPUT_GET, 'start', FILTER_VALIDATE_INT);
		$pos = filter_input(INPUT_GET, 'pos', FILTER_VALIDATE_INT);

		$type = ORM::factory('type');
		$term_id = filter_input(INPUT_GET, 'term_id', FILTER_VALIDATE_INT);
		$term_id = ($term_id) ? $term_id : 0;
		/** 获取指定类型(if have)指定起止位置(if have)的所有类别条数*/
		if($term_id) 
		{
			$total_count = $type->where('term_id', '=', $term_id)->count_all();
			$start = ($start) ? $start : 0;
			$pos = ($pos) ? $pos :$total_count;
			$types = $type
				->where('term_id', '=', $term_id)
				->offset($start)
				->limit($pos)
				->find_all();
		}
		else
		{
			$total_count = PHP_INT_MAX;
			$start = ($start) ? $start : 0;
			$pos = ($pos) ? $pos :$total_count;
			$types = $type
				->offset($start)
				->limit($pos)
				->find_all();
		}
		
		if(!$types -> count()) 
		{
			$this->response->body("不存在所要的类型!")->status(206);	
		}
		else
		{
			$rset['num'] = $types->count();
			$rset['list'] = array();
			foreach($types as $type)
			{
				$rset['list'][] = $type->as_array();
			}
			$json_ret = json_encode($rset);
			$this->response->body($json_ret)->status(200);
		}
	}
	
	/**
	 * @brief 新增type
	 *
	 * @return 
	 */
	public function action_post() 
	{
		$type = ORM::factory('type');
		if(!$_POST) return;
		if(isset($_POST['id']))
		{
			unset($_POST['id']);
		}
		try 
		{
			/** 提交type*/
			$type->values($_POST)->check();
			$type->save();
			$this->response->body('添加类别成功')->status(200);
		}
		catch (ORM_Validation_Exception $e)
		{
			$error = $e->errors();
			foreach($error as $k=>$v)
			{
			 $this->response->body($k.':not available')->status(202);
			}
		}
	}
	/**
	 * @brief 更新指定type
	 *
	 * @param $type_id
	 *
	 * @return $this 
	 */
	public function action_put($type_id) 
	{
		$type = ORM::factory('type', $type_id);
		if(!$type->type_id)
		{
			$this->response->body('类别不存在！')->status(206);
			return;
		}
		try 
		{
			$type->values($this->request->post())->check()->update();
			$this->response->body('更新类别成功！')->status(200);
		}
		catch (ORM_Validation_Exception $e)
		{/** 验证错误处理*/
			$error = $e->errors();
			foreach($error as $k=>$v)
			{
				$this->response->body($k.':not avalilable')->status(206);
			}
		}
		catch(Exception $err)
		{/** 其他错误处理*/
			$this->response->body($err->getMessage())->status(500);
		}
	}
	/**
	 * @brief 删除指定type
	 *
	 * @param $type_id
	 *
	 * @return $this
	 */
	public function action_delete($type_id) 
	{
		$type = ORM::factory('type', $type_id);
		if(!$type->type_id)
		{
			$this->response->body('类别不存在！')->status(206);
		}
		$type->delete();
		$this->response->body('删除类别成功！')->status(200);
	}
}
